package com.bdqn.t350.vue.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.t350.vue.bean.Dto;
import com.bdqn.t350.vue.bean.User;
import com.bdqn.t350.vue.common.constant.Constants;
import com.bdqn.t350.vue.service.UserService;
import com.bdqn.t350.vue.utils.RedisUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    RedisUtil redisUtil;

    @Resource
    UserService userService;

    @CrossOrigin
    @ResponseBody
    @RequestMapping("/findByUserName1")
    public Object findByUserName1(@RequestBody User loginUser) {
        User user = userService.selectByName(loginUser.getUsrName());
        return user;
    }

    @CrossOrigin
    @ResponseBody
    @RequestMapping("/findByUserName")
    @PreAuthorize("hasAuthority('test')")
    public Object findByUserName(@RequestBody User loginUser) {
        User user = userService.selectByName(loginUser.getUsrName());
        return user;
    }

    @CrossOrigin
    @ResponseBody
    @RequestMapping("/findByUserName2")
    @PreAuthorize("hasAuthority('admin')")
    public Object findByUserName2(@RequestBody User loginUser) {
        User user = userService.selectByName(loginUser.getUsrName());
        return user;
    }

    @CrossOrigin
    @ResponseBody
    @RequestMapping("/selectPage")
    public Object selectPage(
            @RequestParam(name = "pageIndex", defaultValue = "1") int index,
            @RequestParam(name = "pageSize", defaultValue = "10") int pageSize,
            @RequestParam(name = "usrName", defaultValue = "") String usrName,
            HttpServletRequest request) {
        Dto dto = new Dto();
        String token = request.getHeader("token");
        if (token == null || token.equals("null")) {
            dto.setErrorCode(Constants.TOKEN_NULL);
            return dto;
        }
        long time = redisUtil.getExpire(token);
        if (time == -2) {
            dto.setErrorCode(Constants.TOKEN_EXPIRE);
            return dto;
        }
        if (time < 60) {
            redisUtil.expire(token, 600);
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like("usr_name", usrName);
        //if (!roleId.equals("")) {
        //    queryWrapper.eq("role_id", roleId);
        //}
        IPage<User> page = new Page<>();
        page.setCurrent(index);//当前页
        page.setSize(pageSize);//每页显示多少条记录
        userService.selectPage(page, queryWrapper);
        dto.setData(page);
        dto.setToken(token);
        dto.setSuccess("true");
        return dto;
    }

    @CrossOrigin
    @ResponseBody
    @RequestMapping("/deleteById")
    public Object deleteById(int id, HttpServletRequest request) {
        Dto dto = new Dto();
        String token = request.getHeader("token");
        if (token == null || token.equals("null")) {
            dto.setErrorCode(Constants.TOKEN_NULL);
            return dto;
        }
        long time = redisUtil.getExpire(token);
        if (time == -2) {
            dto.setErrorCode(Constants.TOKEN_EXPIRE);
            return dto;
        }
        if (time < 60) {
            redisUtil.expire(token, 600);
        }
        if (userService.deleteById(id) > 0)
            dto.setSuccess("true");
        else
            dto.setSuccess("false");
        dto.setToken(token);
        dto.setSuccess("true");
        return dto;
    }

    @CrossOrigin
    @ResponseBody
    @PostMapping("/add")
    public Object add(@RequestBody User user, HttpServletRequest request) {
        Dto dto = new Dto();
        String token = request.getHeader("token");
        if (token == null || token.equals("null")) {
            dto.setErrorCode(Constants.TOKEN_NULL);
            return dto;
        }
        long time = redisUtil.getExpire(token);
        if (time == -2) {
            dto.setErrorCode(Constants.TOKEN_EXPIRE);
            return dto;
        }
        if (time < 60) {
            redisUtil.expire(token, 600);
        }
        if (userService.insert(user) > 0)
            dto.setSuccess("true");
        else
            dto.setSuccess("false");
        dto.setToken(token);
        dto.setSuccess("true");
        return dto;
    }

    @CrossOrigin
    @ResponseBody
    @PostMapping("/update")
    public Object update(@RequestBody User user, HttpServletRequest request) {
        Dto dto = new Dto();
        String token = request.getHeader("token");
        if (token == null || token.equals("null")) {
            dto.setErrorCode(Constants.TOKEN_NULL);
            return dto;
        }
        long time = redisUtil.getExpire(token);
        if (time == -2) {
            dto.setErrorCode(Constants.TOKEN_EXPIRE);
            return dto;
        }
        if (time < 60) {
            redisUtil.expire(token, 600);
        }
        if (userService.updateById(user) > 0)
            dto.setSuccess("true");
        else
            dto.setSuccess("false");
        dto.setToken(token);
        dto.setSuccess("true");
        return dto;
    }
}
